package com.bulunsh.wensen.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {


    @RequestMapping(value = "/login-success",produces = "text/plain;charset=UTF-8")
    public String success(){

        return getUsername()+"登录成功";
    }

    @RequestMapping(value = "/r/r1",produces = "text/plain;charset=UTF-8")
    public String r1(){

        return getUsername()+"r1";
    }

    @RequestMapping(value = "/r/r2",produces = "text/plain;charset=UTF-8")
    public String r2(){

        return getUsername()+"r2";
    }

    //获取当前用户信息
    private String getUsername(){
        String username = "";
        //当前通过认证的用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //用户身份
        Object  principal = authentication.getPrincipal();
        if (principal == null) {
            username = "匿名";
        }
        if (principal instanceof UserDetails) {
            UserDetails userDetails = (UserDetails)principal;
            username = userDetails.getUsername();
        } else {
            username = principal.toString();
        }
        return username;
    }
}
